﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BTv2.Controls;
using BTv2.Model;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for AdminMailing.xaml
    /// </summary>
    public partial class AdminMailing : UserControl
    {
        private Settings _AdminSettings;
        private UsersController _ucontroller;
        public AdminMailing()
        {
            InitializeComponent();
        }

        public AdminMailing(Settings s)
        {
            InitializeComponent();
            _AdminSettings = s;
        }
        private void ContentLoaded(object sender, RoutedEventArgs e)
        {
            _ucontroller = UsersController.GetInstance();
            MailListBox.ItemsSource = _ucontroller.MLists;
            UsersListBox.ItemsSource = _ucontroller.Users;
        }



        private async void MailListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var ml = MailListBox.SelectedItem as MailingList;
            try
            {

                ml.BackupUsers = new ObservableCollection<User>(ml.Users);
                CurrentMlistContainer.IsEnabled = true;
                CurrentMlistContainer.DataContext = ml;
                UsersListBox.IsEnabled = true;
                MailListBox.IsEnabled = false;
            }
            catch
            {
                //when deselecting its called
            }
            try
            {
                UsersListBox.ItemsSource = await _ucontroller.GetFilteredUserList(ml);
            }
            catch
            {
                UsersListBox.ItemsSource = _ucontroller.Users;
            }
        }

        ListBox dragSource = null;

        private void UsersListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            dragSource = parent;
            object data = GetDataFromListBox(dragSource, e.GetPosition(parent));

            if (data != null)
            {
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
            }
        }
        private static object GetDataFromListBox(ListBox source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);
                    if (data == DependencyProperty.UnsetValue)
                    {
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    }
                    if (element == source)
                    {
                        return null;
                    }
                }
                if (data != DependencyProperty.UnsetValue)
                {
                    return data;
                }
            }
            return null;
        }

        private void CurrentMlistUserList_Drop(object sender, DragEventArgs e)
        {
            ListBox parent = (ListBox)sender;
            User data = e.Data.GetData(typeof(User)) as User;
            ((IList)dragSource.ItemsSource).Remove(data);
            (parent.ItemsSource as ObservableCollection<User>).Add(data);
        }

        private void CMLCancelButton_Click(object sender, RoutedEventArgs e)
        {
            MailListBox.UnselectAll();
            var ml = CurrentMlistContainer.DataContext as MailingList;
            ml.Users = ml.BackupUsers;
            CurrentMlistContainer.DataContext = null;
            UsersListBox.IsEnabled = false;
            MailListBox.IsEnabled = true;
            CurrentMlistContainer.IsEnabled = false;
        }

        public async void CMLAddMailingListButton(object sender, RoutedEventArgs e)
        {
            MailingListMenu.IsOpen = false;
            CurrentMlistContainer.IsEnabled = true;
            CurrentMlistContainer.DataContext = new MailingList() { ID = 0, Users = new ObservableCollection<User>(), BackupUsers = new ObservableCollection<User>() };
            UsersListBox.IsEnabled = true;
            MailListBox.IsEnabled = false;
        }

        private async void CMLDeleteButton_Click(object sender, RoutedEventArgs e)
        {
            MailingList _ml = CurrentMlistContainer.DataContext as MailingList;
            bool result = await _ucontroller.DeleteMailingList(_ml);
            if (result)
            {
                _ucontroller.MLists.Remove(_ml);

            }
            MailListBox.UnselectAll();
            CurrentMlistContainer.DataContext = null;
            UsersListBox.IsEnabled = false;
            MailListBox.IsEnabled = true;
            CurrentMlistContainer.IsEnabled = false;
        }

        private async void CMLSaveButton_Click(object sender, RoutedEventArgs e)
        {
            MailingList _ml = CurrentMlistContainer.DataContext as MailingList;
            int mlid = _ml.ID;
            bool result = await _ucontroller.SaveMailingList(_ml, CMLNameTextBox.Text, CMLDescrptionTextBox.Text);
            if (result)
            {
                _ml.Name = CMLNameTextBox.Text;
                _ml.Description = CMLDescrptionTextBox.Text;
                if (mlid == 0)
                {
                    _ucontroller.MLists.Add(_ml);
                }
            }
            MailListBox.UnselectAll();
            CurrentMlistContainer.DataContext = null;
            UsersListBox.IsEnabled = false;
            MailListBox.IsEnabled = true;
            CurrentMlistContainer.IsEnabled = false;
        }

        private void OpenMailingListMenu(object sender, MouseButtonEventArgs e)
        {
            MailingListMenu.IsOpen = !MailingListMenu.IsOpen;
        }


        private void BackButtonMenu(object sender, RoutedEventArgs e)
        {
            _AdminSettings.OpenAdminSettings();
        }
    }
}
